package org.redis.cluster.loadbalance;

import java.util.Arrays;
import java.util.Collection;

import org.apache.commons.collections.CollectionUtils;
import org.redis.cluster.config.ClientConfig;
import org.redis.cluster.redis.RedisCommand;

public class HashLoadBalance implements LoadBalance {

	@Override
	public ClientConfig select(Collection<ClientConfig> configs,RedisCommand command) {
		int hash=getHashCode(command);
		return (ClientConfig) CollectionUtils.get(configs, Math.abs(hash)%configs.size());
	}

	protected int getHashCode(RedisCommand command) {
		return getHashCode(command.getParameters()[1]);
	}
	
	protected int getHashCode(byte bytes[]) {
		return Arrays.hashCode(bytes);
	}

}